package cn.dcw.pethome.pet.service.impl;

import cn.dcw.pethome.basic.exception.MyException;
import cn.dcw.pethome.basic.service.impl.BaseServiceImpl;
import cn.dcw.pethome.basic.util.DistanceUtil;
import cn.dcw.pethome.basic.util.LoginContext;
import cn.dcw.pethome.basic.util.PageList;
import cn.dcw.pethome.basic.util.Point;
import cn.dcw.pethome.org.domain.Employee;
import cn.dcw.pethome.org.domain.Shop;
import cn.dcw.pethome.org.mapper.EmployeeMapper;
import cn.dcw.pethome.org.mapper.ShopMapper;
import cn.dcw.pethome.pet.domain.Pet;
import cn.dcw.pethome.pet.domain.PetDetail;
import cn.dcw.pethome.pet.domain.SearchMasterMsg;
import cn.dcw.pethome.pet.mapper.PetDetailMapper;
import cn.dcw.pethome.pet.mapper.PetMapper;
import cn.dcw.pethome.pet.mapper.SearchMasterMsgMapper;
import cn.dcw.pethome.pet.query.SearchMasterMsgQuery;
import cn.dcw.pethome.pet.service.ISearchMasterMsgService;
import cn.dcw.pethome.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    /**
     * 宠物寻主
     * @param masterMsg
     */
    @Override
    public void add(SearchMasterMsg masterMsg,HttpServletRequest request) {

        //1.调用工具类，获取登入用户信息
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        if(loginInfo==null){
            throw new MyException("亲请重新登入***");
        }

        //2.获取用户所在地最近的门店信息
        List<Shop> shops = shopMapper.findAll();//所有门店信息
        String address = masterMsg.getAddress();
        Point point = DistanceUtil.getPoint(address);
        //得到最近的门店信息
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        //准备数据，储存到数据库
        masterMsg.setUser_id(loginInfo.getId());
        masterMsg.setShop_id(shop.getId());
        masterMsg.setState(0);
        //调用mapper将数据储存到数据库
        searchMasterMsgMapper.add(masterMsg);

    }

    /**
     * 已处理宠物寻主信息
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        //状态为1，为已处理
        query.setState(1);
        //通过LoginInfoId查询员工对象
        Employee employee = employeeMapper.findByLoginInfoId(id);
        if(employee!=null){
            if(employee.getLogininfo_id()!=null){//店家
                query.setShopId(employee.getShop_id());//只能看到自己对应店铺信息
                //调用父类的方法 返回信息
                return super.pageData(query);
            }
        }
        //Shop_id为null时返回的结果
        return super.pageData(query);
    }

    /**
     * 未处理宠物寻主信息
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        //状态为0，为未处理
        query.setState(0);
        //通过LoginInfoId查询员工对象
        Employee employee = employeeMapper.findByLoginInfoId(id);
        if(employee!=null){
            if(employee.getShop_id()!=null){//店家
                query.setShopId(employee.getShop_id());//只能看到自己对应店铺信息
                //调用父类的方法 返回信息
                return super.pageData(query);
            }
        }
        //Shop_id为null时返回的结果
        return super.pageData(query);
    }

    /**
     * 处理寻主
     * @param pet
     */
    @Transactional
    @Override
    public void handle(Pet pet,HttpServletRequest Request) {

        //获取用户信息
        LoginInfo loginInfo = LoginContext.getLoginInfo(Request);
        if(loginInfo!=null){//用户信息redis中还没过期时，操作以下代码
            //1.1将状态改为1
            searchMasterMsgMapper.updateStateByMsgId(pet.getSearch_master_msg_id());
            //2.并且将新的数据添加到pet表
            //Long loginInfoId = loginInfo.getId();
            //得到登入操作用户
            //Employee loginUser = employeeMapper.findByLoginInfoId(loginInfoId);

            petMapper.add(pet);
            //获取宠物详情对象,并吧数据储存到数据库表中
            PetDetail detail = pet.getDetail();

            if(detail!=null){
                detail.setPet_id(pet.getId());
                petDetailMapper.add(detail);
            }
        }else {
            throw new MyException("请重新登入***");
        }
    }


    //    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
